*A) LINE LEVEL - PRODUCTIVITY & OUTPUT
cd "C:\EDCC revision\Final\Stata code & data\"
use quantity.dta, clear

egen time=group(edate)
g date_1=mdy(8, 1, 2013)+time-1
format date %td
sort time

*generate line index
g line_index=.
recode line_index .=1 if dummy11==1
recode line_index .=2 if dummy12==1
recode line_index .=3 if dummy21==1
recode line_index .=4 if dummy22==1
recode line_index .=5 if dummy31==1
recode line_index .=6 if dummy32==1
recode line_index .=7 if dummy41==1
recode line_index .=8 if dummy51==1
recode line_index .=9 if dummy61==1
la def line_index 1 "LINE 1A" 2 "LINE 1B" 3 "LINE 2A" 4 "LINE 2B" 5 "LINE 3A" 6 "LINE 3B" 7 "LINE 4" 8 "LINE 5" 9 "LINE 6", replace
la val line_index line_index

*generate dependent variables
*1) production
g prod=exp(lnprod)
recode prod 1=0
*2) output per worker
g workers=exp(lnworkers)
recode workers 1=0
*3) Menzel (2021) definition of productivity
bys line_index: g productivity=(prod*smv)/(worker*8*60)
g lproductivity=log(productivity+1)
*4) target efficiency
bys line_index: g tareff=(target*smv)/(worker*8*60)
g ltareff=log(tareff+1)

*counter either side of QMP
g counter=.
replace counter=time-343 if QMP==1
*replace counter=342-time if QMP==0
replace counter=344-time if QMP==0

cd "C:\EDCC revision\Final\Stata code & data\"
*TABLE 1
log using table1.log, replace
su workers smv target prod avgprod tareff productivity
log close

*TABLE 2
log using table2.log, replace
bys line_index: su smv
log close

log using table3.log, replace
*TABLE 3 - pooled OLS wth Driscoll and Kraay standard errors
*output - FE
xtscc lnprod lnworker lnmaterials lnsmv lntarget QMP timedummy2-timedummy4, fe
*labour productivity - FE
xtscc lnavgprod lnmaterials lnsmv lntarget QMP timedummy2-timedummy4, fe
*Menzel productivity - FE
xtscc lproductivity lnmaterials lntarget QMP timedummy2-timedummy4, fe
*efficiency - FE
xtscc ltareff lnmaterials QMP timedummy2-timedummy4, fe
log close

*QMP across window graphically - old Table 4- FE
g win=0
recode win 0=1 if counter<=15
recode win 0=2 if counter<=30
recode win 0=3 if counter<=45
recode win 0=4 if counter<=60
recode win 0=5 if counter<=75
recode win 0=6 if counter<=90
recode win 0=7 if counter<=105
recode win 0=8 if counter<=120
recode win 0=9 if counter<=135
recode win 0=10 if counter<=150
recode win 0=11 if counter<=165
recode win 0=12 if counter<=180
recode win 0=13 if counter<=195
recode win 0=14 if counter<=210
recode win 0=15 if counter<=225
recode win 0=16 if counter<=240
recode win 0=17 if counter<=255
recode win 0=18 if counter<=270
recode win 0=19 if counter<=285
recode win 0=20 if counter<=300
recode win 0=21 if counter<=315
recode win 0=22 if counter<=330
replace win=. if (counter~=1 | counter>330) & win==0
recode win 1=0 if counter<=5
la def win 0 "5" 1"15" 2"30" 3"45" 4"60" 5 "75" 6 "90" 7 "105" 8 "120" 9 "135" 10 "150" 11 "165" 12 "180" 13 "195" 14 "210" 15 "225" ///
16 "240" 17 "255" 18 "270" 19 "285" 20 "300" 21 "315" 22 "330", replace
la val win win

*TABLE 4
log using "table4.log", replace
*production - overall line FE
xtscc lnprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=5, fe
xtscc lnprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=15, fe
xtscc lnprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=30, fe
xtscc lnprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=45, fe
xtscc lnprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=60, fe
xtscc lnprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=75, fe
xtscc lnprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=90, fe
xtscc lnprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=105, fe
xtscc lnprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=120, fe
xtscc lnprod lnworkers lnmaterials lnsmv lntarget c.win##ib(0).QMP timedummy2-timedummy4 if counter>=0 & counter<=330, fe
margins, dydx(QMP) at(win=(0 1 2 3 4 5 6 7 8)) 

*Labour productivity - overall line FE
xtscc lnavgprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=5, fe
xtscc lnavgprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=15, fe
xtscc lnavgprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=30, fe
xtscc lnavgprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=45, fe
xtscc lnavgprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=60, fe
xtscc lnavgprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=75, fe
xtscc lnavgprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=90, fe
xtscc lnavgprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=105, fe
xtscc lnavgprod lnworkers lnmaterials lnsmv lntarget QMP if counter<=120, fe
xtscc lnavgprod lnmaterials lnsmv lntarget lnsmv lntarget c.win##ib(0).QMP timedummy2-timedummy4 if counter>=1 & counter<=330, fe
margins, dydx(QMP) at(win=(0 1 2 3 4 5 6 7 8)) 

*Efficiency - overall line FE
xtscc ltareff lnworkers lnmaterials lnsmv lntarget QMP if counter<=5, fe
xtscc ltareff lnworkers lnmaterials lnsmv lntarget QMP if counter<=15, fe
xtscc ltareff lnworkers lnmaterials lnsmv lntarget QMP if counter<=30, fe
xtscc ltareff lnworkers lnmaterials lnsmv lntarget QMP if counter<=45, fe
xtscc ltareff lnworkers lnmaterials lnsmv lntarget QMP if counter<=60, fe
xtscc ltareff lnworkers lnmaterials lnsmv lntarget QMP if counter<=75, fe
xtscc ltareff lnworkers lnmaterials lnsmv lntarget QMP if counter<=90, fe
xtscc ltareff lnworkers lnmaterials lnsmv lntarget QMP if counter<=105, fe
xtscc ltareff lnworkers lnmaterials lnsmv lntarget QMP if counter<=120, fe
xtscc ltareff lnmaterials c.win##ib(0).QMP timedummy2-timedummy4 if counter>=1 & counter<=330, fe
margins, dydx(QMP) at(win=(0 1 2 3 4 5 6 7 8)) 

*Productivity - overall line FE
xtscc lproductivity lnworkers lnmaterials lnsmv lntarget QMP if counter<=5, fe
xtscc lproductivity lnworkers lnmaterials lnsmv lntarget QMP if counter<=15, fe
xtscc lproductivity lnworkers lnmaterials lnsmv lntarget QMP if counter<=30, fe
xtscc lproductivity lnworkers lnmaterials lnsmv lntarget QMP if counter<=45, fe
xtscc lproductivity lnworkers lnmaterials lnsmv lntarget QMP if counter<=60, fe
xtscc lproductivity lnworkers lnmaterials lnsmv lntarget QMP if counter<=75, fe
xtscc lproductivity lnworkers lnmaterials lnsmv lntarget QMP if counter<=90, fe
xtscc lproductivity lnworkers lnmaterials lnsmv lntarget QMP if counter<=105, fe
xtscc lproductivity lnworkers lnmaterials lnsmv lntarget QMP if counter<=120, fe
xtscc lproductivity lnmaterials lntarget c.win##ib(0).QMP timedummy2-timedummy4 if counter>=1 & counter<=330, fe
margins, dydx(QMP) at(win=(0 1 2 3 4 5 6 7 8)) 
log close

*Production - overall line FE
xtscc lnprod lnworkers lnmaterials lnsmv lntarget c.win##ib(0).QMP timedummy2-timedummy4 if counter>=0 & counter<=330, fe
margins, dydx(QMP) at(win=(0 1 2 3 4 5 6 7 8)) 
marginsplot, xtitle(output) xtitle(, size(small) margin(small) color(black)) ytitle(Effect of QMP) ///
ytitle(, size(small) margin(small) color(black)) ylabel(, labsize(vsmall)) ///
xtitle(days pre/post intervention) xtitle(,size(small) margin(small) color(black)) xlabel(, labsize(vsmall) angle (vertical)) yline(0, lcolor(red)) ///
title(, size(small) color(black)) plotopts(mcolor(blue) lcolor(blue) msymbol(X)) ciopts(lcolor(gs10)) byopt(title(" "))
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\window_production.gph", replace

*Labour productivity - overall line FE
xtscc lnavgprod lnmaterials lnsmv lntarget lnsmv lntarget c.win##ib(0).QMP timedummy2-timedummy4 if counter>=1 & counter<=330, fe
margins, dydx(QMP) at(win=(0 1 2 3 4 5 6 7 8)) 
marginsplot, xtitle(output) xtitle(, size(small) margin(small) color(black)) ytitle(Effect of QMP) ///
ytitle(, size(small) margin(small) color(black)) ylabel(, labsize(vsmall)) ///
xtitle(days pre/post intervention) xtitle(,size(small) margin(small) color(black)) xlabel(, labsize(vsmall) angle(vertical)) yline(0, lcolor(red)) ///
title(, size(small) color(black)) plotopts(mcolor(blue) lcolor(blue) msymbol(X)) ciopts(lcolor(gs10)) byopt(title(" "))
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\window_output-per-worker.gph", replace

*Efficiency - overall line FE
xtscc ltareff lnmaterials c.win##ib(0).QMP timedummy2-timedummy4 if counter>=1 & counter<=330, fe
margins, dydx(QMP) at(win=(0 1 2 3 4 5 6 7 8)) 
marginsplot, xtitle(output) xtitle(, size(small) margin(small) color(black)) ytitle(Effect of QMP) ///
ytitle(, size(small) margin(small) color(black)) ylabel(, labsize(vsmall)) ///
xtitle(days pre/post intervention) xtitle(,size(small) margin(small) color(black)) xlabel(, labsize(vsmall) angle(vertical)) yline(0, lcolor(red)) ///
title(, size(small) color(black)) plotopts(mcolor(blue) lcolor(blue) msymbol(X)) ciopts(lcolor(gs10)) byopt(title(" "))
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\window_target-efficiency.gph", replace

*Productivity - overall line FE
xtscc lproductivity lnmaterials lntarget c.win##ib(0).QMP timedummy2-timedummy4 if counter>=1 & counter<=330, fe
margins, dydx(QMP) at(win=(0 1 2 3 4 5 6 7 8)) 
marginsplot, xtitle(output) xtitle(, size(small) margin(small) color(black)) ytitle(Effect of QMP) ///
ytitle(, size(small) margin(small) color(black)) ylabel(, labsize(vsmall)) ///
xtitle(days pre/post intervention) xtitle(,size(small) margin(small) color(black)) xlabel(, labsize(vsmall) angle(vertical)) yline(0, lcolor(red)) ///
title(, size(small) color(black)) plotopts(mcolor(blue) lcolor(blue) msymbol(X)) ciopts(lcolor(gs10)) byopt(title(" "))
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\window_productivity.gph", replace

*FIGURE 1
graph combine "window_production.gph" "window_output-per-worker.gph" "window_target-efficiency.gph" "window_productivity.gph" 
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\\Figure 1.gph", replace

*Production by line
local i=1
while `i'<=9 {
	xtscc lnprod lnworkers lnmaterials lnsmv lntarget c.win##ib(0).QMP timedummy2-timedummy4 if counter>=1 & counter<=330 & line_index==`i', fe
margins, dydx(QMP) at(win=(0 1 2 3 4 5 6 7 8)) 
marginsplot, xtitle(output) xtitle(, size(small) margin(small) color(black)) ytitle(Effect of QMP) ///
ytitle(, size(small) margin(small) color(black)) ylabel(, labsize(vsmall)) ///
xtitle(days pre/post intervention) xtitle(,size(small) margin(small) color(black)) xlabel(, labsize(vsmall) angle (vertical)) yline(0, lcolor(red)) ///
title(LINE `i', size(small) color(black)) plotopts(mcolor(blue) lcolor(blue) msymbol(X)) ciopts(lcolor(gs10)) byopt(title(" "))
graph save output_window_`i'.gph, replace
	local i=`i'+1
}
graph combine "output_window_1.gph" "output_window_2.gph" "output_window_3.gph" "output_window_4.gph" "output_window_5.gph" "output_window_6.gph" "output_window_7.gph" "output_window_8.gph" "output_window_9.gph"
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\Figure 1A.gph", replace

*Labour productivity by line
local i=1
while `i'<=9 {
	xtscc lnavgprod lnmaterials lnsmv lntarget c.win##ib(0).QMP timedummy2-timedummy4 if counter>=1 & counter<=330 & line_index==`i', fe
margins, dydx(QMP) at(win=(0 1 2 3 4 5 6 7 8)) 
marginsplot, xtitle(output) xtitle(, size(small) margin(small) color(black)) ytitle(Effect of QMP) ///
ytitle(, size(small) margin(small) color(black)) ylabel(, labsize(vsmall)) ///
xtitle(days pre/post intervention) xtitle(,size(small) margin(small) color(black)) xlabel(, labsize(vsmall) angle(vertical)) yline(0, lcolor(red)) ///
title(LINE `i', size(small) color(black)) plotopts(mcolor(blue) lcolor(blue) msymbol(X)) ciopts(lcolor(gs10)) byopt(title(" "))
graph save opw_window_`i'.gph, replace
	local i=`i'+1
}
graph combine "opw_window_1.gph" "opw_window_2.gph" "opw_window_3.gph" "opw_window_4.gph" "opw_window_5.gph" "opw_window_6.gph" "opw_window_7.gph" "opw_window_8.gph" "opw_window_9.gph"
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\Figure 1B.gph", replace

*Efficiency by line
local i=1
while `i'<=9 {
	xtscc ltareff lnmaterials c.win##ib(0).QMP timedummy2-timedummy4 if counter>=1 & counter<=330 & line_index==`i', fe
margins, dydx(QMP) at(win=(0 1 2 3 4 5 6 7 8)) 
marginsplot, xtitle(output) xtitle(, size(small) margin(small) color(black)) ytitle(Effect of QMP) ///
ytitle(, size(small) margin(small) color(black)) ylabel(, labsize(vsmall)) ///
xtitle(days pre/post intervention) xtitle(,size(small) margin(small) color(black)) xlabel(, labsize(vsmall) angle(vertical)) yline(0, lcolor(red)) ///
title(LINE `i', size(small) color(black)) plotopts(mcolor(blue) lcolor(blue) msymbol(X)) ciopts(lcolor(gs10)) byopt(title(" "))
graph save eff_window_`i'.gph, replace
	local i=`i'+1
}
graph combine "eff_window_1.gph" "eff_window_2.gph" "eff_window_3.gph" "eff_window_4.gph" "eff_window_5.gph" "eff_window_6.gph" "eff_window_7.gph" "eff_window_8.gph" "eff_window_9.gph"
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\Figure 1C.gph", replace

*Productivity by line
local i=1
while `i'<=9 {
	xtscc lproductivity lnmaterials lntarget c.win##ib(0).QMP timedummy2-timedummy4 if counter>=1 & counter<=330 & line_index==`i', fe
margins, dydx(QMP) at(win=(0 1 2 3 4 5 6 7 8)) 
marginsplot, xtitle(output) xtitle(, size(small) margin(small) color(black)) ytitle(Effect of QMP) ///
ytitle(, size(small) margin(small) color(black)) ylabel(, labsize(vsmall)) ///
xtitle(days pre/post intervention) xtitle(,size(small) margin(small) color(black)) xlabel(, labsize(vsmall) angle(vertical)) yline(0, lcolor(red)) ///
title(LINE `i', size(small) color(black)) plotopts(mcolor(blue) lcolor(blue) msymbol(X)) ciopts(lcolor(gs10)) byopt(title(" "))
graph save product_window_`i'.gph, replace
	local i=`i'+1
}
graph combine "product_window_1.gph" "product_window_2.gph" "product_window_3.gph" "product_window_4.gph" "product_window_5.gph" "product_window_6.gph" "product_window_7.gph" "product_window_8.gph" "product_window_9.gph"
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\Figure 1D.gph", replace

*TABLE 5
log using table5.log, replace
*1) output 
xtscc lnprod lnworkers lnmaterials lnsmv lntarget ib(1).line_index##ib(0).QMP timedummy2-timedummy4, fe
margins, dydx(QMP) over(line_index)
*2) labour productivity
xtscc lnavgprod lnmaterials lnsmv lntarget ib(1).line_index##ib(0).QMP timedummy2-timedummy4, fe
margins, dydx(QMP) over(line_index)
*3) productivity
xtscc lproductivity lnmaterials lntarget ib(1).line_index##ib(0).QMP timedummy2-timedummy4, fe
margins, dydx(QMP) over(line_index)
*4) efficiency
xtscc ltareff lnmaterials ib(1).line_index##ib(0).QMP timedummy2-timedummy4, fe
margins, dydx(QMP) over(line_index)
log close

*Effects of QMP over SMV
g r=round(smv)

*LINE specific effects
local i=1
while `i'<=9 {
	xtpcse lnprod lnworkers lnmaterials lntarget c.r##ib(0).QMP timedummy2-timedummy4 if line_index==`i', correlation(psar1)
margins, dydx(QMP) over(r)
marginsplot, xtitle(output) xtitle(, size(small) margin(small) color(black)) ytitle(Effect of QMP) ///
ytitle(, size(small) margin(small) color(black)) ylabel(, labsize(small)) ///
xtitle(smv) xtitle(,size(small) margin(small) color(black)) xlabel(, labsize(small)) yline(0, lcolor(red)) ///
title(, size(small) color(black)) plotopts(mcolor(blue) lcolor(blue) msymbol(X)) ciopts(lcolor(gs10)) byopt(title(" "))
graph save output_`i'.gph, replace
	local i=`i'+1
}
*FIGURE 2
graph combine "output_1.gph" "output_2.gph" "output_3.gph" "output_4.gph" "output_5.gph" "output_6.gph" "output_7.gph" "output_8.gph" "output_9.gph"
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\F2 net output SMV.gph", replace

local i=1
while `i'<=9 {
	xtpcse lnavgprod lnmaterials lntarget c.r##c.r##ib(0).QMP timedummy2-timedummy4 if line_index==`i', correlation(psar1)
margins, dydx(QMP) over(r)
marginsplot, xtitle(output) xtitle(, size(small) margin(small) color(black)) ytitle(Effect of QMP) ///
ytitle(, size(small) margin(small) color(black)) ylabel(, labsize(small)) ///
xtitle(smv) xtitle(,size(small) margin(small) color(black)) xlabel(, labsize(small)) yline(0, lcolor(red)) ///
title(, size(small) color(black)) plotopts(mcolor(blue) lcolor(blue) msymbol(X)) ciopts(lcolor(gs10)) byopt(title(" "))
graph save opw_`i'.gph, replace
	local i=`i'+1
}
graph combine "opw_1.gph" "opw_2.gph" "opw_3.gph" "opw_4.gph" "opw_5.gph" "opw_6.gph" "opw_7.gph" "opw_8.gph" "opw_9.gph"
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\F2 net labour productivity SMV.gph", replace
* used for Figure 2

local i=1
while `i'<=9 {
	xtpcse lproductivity lnmaterials lntarget c.r##ib(0).QMP time timesq if line_index==`i', correlation(psar1)
margins, dydx(QMP) over(r)
marginsplot, xtitle(output) xtitle(, size(small) margin(small) color(black)) ytitle(Effect of QMP) ///
ytitle(, size(small) margin(small) color(black)) ylabel(, labsize(small)) ///
xtitle(smv) xtitle(,size(small) margin(small) color(black)) xlabel(, labsize(small)) yline(0, lcolor(red)) ///
title(, size(small) color(black)) plotopts(mcolor(blue) lcolor(blue) msymbol(X)) ciopts(lcolor(gs10)) byopt(title(" "))
graph save productivity_`i'.gph, replace
	local i=`i'+1
}
graph combine "productivity_1.gph" "productivity_2.gph" "productivity_3.gph" "productivity_4.gph" "productivity_5.gph" "productivity_6.gph" "productivity_7.gph" "productivity_8.gph" "productivity_9.gph"
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\\F2 net productivity SMV.gph", replace

local i=1
while `i'<=9 {
	xtpcse ltareff lnmaterials c.r##ib(0).QMP timedummy2-timedummy4 if line_index==`i', correlation(psar1)
margins, dydx(QMP) over(r)
marginsplot, xtitle(output) xtitle(, size(small) margin(small) color(black)) ytitle(Effect of QMP) ///
ytitle(, size(small) margin(small) color(black)) ylabel(, labsize(small)) ///
xtitle(smv) xtitle(,size(small) margin(small) color(black)) xlabel(, labsize(small)) yline(0, lcolor(red)) ///
title(, size(small) color(black)) plotopts(mcolor(blue) lcolor(blue) msymbol(X)) ciopts(lcolor(gs10)) byopt(title(" "))
graph save efficiency_`i'.gph, replace
	local i=`i'+1
}
graph combine "efficiency_1.gph" "efficiency_2.gph" "efficiency_3.gph" "efficiency_4.gph" "efficiency_5.gph" "efficiency_6.gph" "efficiency_7.gph" "efficiency_8.gph" "efficiency_9.gph"
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\F2 net efficiency SMV.gph", replace

*TABLE 6
*Difference-in-Difference
keep if line==1 | line==6
g treated=0
recode treated 0=1 if line==1
g indicate=1.treated*1.QMP
log using table6.log, replace
*D-i-D analysis
*1) output
areg lnprod lnworker lnmaterials lnsmv lntarget timedummy2-timedummy4 QMP, absorb(line_index)
didregress (lnprod lnworker lnmaterials lnsmv lntarget) (indicate), group(line_index) time(time) aggregate(standard) vce(hc2)
estat ptrends
estat granger
*2) output per worker
areg lnavgprod lnmaterials lnsmv lntarget timedummy2-timedummy4 QMP, absorb(line_index)
didregress (lnavgprod lnmaterials lnsmv lntarget) (indicate), group(line_index) time(time) aggregate(standard) vce(hc2)
estat ptrends
estat granger
*3) productivity
areg lproductivity lnmaterials lntarget timedummy2-timedummy4 QMP, absorb(line_index)
didregress (lproductivity lnmaterials lntarget) (indicate), group(line_index) time(time) aggregate(standard) vce(hc2)
estat ptrends
estat granger
*4) target efficiency
areg ltareff lnmaterials timedummy2-timedummy4 QMP, absorb(line_index)
didregress (ltareff lnmaterials) (indicate), group(line_index) time(time) aggregate(standard) vce(hc2)
estat ptrends
estat granger
log close

*FIGURE A1
*RESTUD HonestDiD
g month=1 if time<=30
replace month=2 if time>30 & time<=60
replace month=3 if time>60 & time<=90
replace month=4 if time>90 & time<=120
replace month=5 if time>120 & time<=150
replace month=6 if time>150 & time<=180
replace month=7 if time>180 & time<=210
replace month=8 if time>210 & time<=240
replace month=9 if time>240 & time<=270
replace month=10 if time>270 & time<=300
replace month=11 if time>300 & time<=330
replace month=12 if time>330 & time<=360
replace month=13 if time>360 & time<=390
replace month=14 if time>390 & time<=420
replace month=15 if time>420 & time<=450
replace month=16 if time>450 & time<=480
replace month=17 if time>480 & time<=510
replace month=18 if time>510 & time<=540
replace month=19 if time>540 & time<=570
replace month=20 if time>570 & time<=600
replace month=21 if time>600 & time<=630
replace month=22 if time>630 & time<=660
replace month=23 if time>660 & time<=690
replace month=24 if time>690 & time<=720
replace month=25 if time>720 & time<=750
replace month=26 if time>750 & time<=780
replace month=27 if time>780 & time<=810
replace month=28 if time>810 & time<=840
replace month=29 if time>840 & time<=870


g Dtime = cond(QMP, time, 342)

reghdfe lnprod b342.Dtime, absorb(line_index month) noconstant
honestdid, numpre(60) mvc(0.5(0.5)2) alpha(0.8) omit mvec(0.5 1 1.5 2) delta(sd)
honestdid, coefplot cached
local plotopts xtitle(Mbar) ytitle(95% Robust CI)
honestdid, cached coefplot `plotopts'
cd "C:\EDCC revision\Final\Stata code & data\"
graph save honest_prod.gph, replace

reghdfe lnavgprod b342.Dtime, absorb(line_index month) noconstant
honestdid, numpre(90) mvc(0.5(0.5)2) alpha(0.9) omit mvec(0.5 1 1.5 2) delta(sd)
honestdid, coefplot cached
local plotopts xtitle(Mbar) ytitle(95% Robust CI)
honestdid, cached coefplot `plotopts'
cd "C:\EDCC revision\Final\Stata code & data\"
graph save honest_labprod.gph, replace

reghdfe lproductivity b342.Dtime, absorb(line_index month) noconstant
honestdid, numpre(60) mvc(0.5(0.5)2) alpha(0.9) omit mvec(0.5 1 1.5 2) delta(sd)
honestdid, coefplot cached
local plotopts xtitle(Mbar) ytitle(95% Robust CI)
honestdid, cached coefplot `plotopts'
cd "C:\EDCC revision\Final\Stata code & data\"
graph save honest_productivity.gph, replace

reghdfe ltareff b342.Dtime, absorb(line_index month) noconstant
honestdid, numpre(60) mvc(0.5(0.5)2) alpha(0.8) omit mvec(0.5 1 1.5 2) delta(sd)
honestdid, coefplot cached
local plotopts xtitle(Mbar) ytitle(95% Robust CI)
honestdid, cached coefplot `plotopts'
cd "C:\EDCC revision\Final\Stata code & data\"
graph save honest_efficiency.gph, replace

graph combine "C:\EDCC revision\Final\Stata code & data\honest_prod.gph" ///
"C:\EDCC revision\Final\Stata code & data\honest_labprod.gph" ///
"C:\EDCC revision\Final\Stata code & data\honest_productivity.gph" ///
"C:\EDCC revision\Final\Stata code & data\honest_efficiency.gph" 
graph save FigureA1.gph, replace

******************************************************************************************************************************************************************************************

*B) DAILY QUALITY DEFECTS
clear all
cd "C:\EDCC revision\Final\Stata code & data"
*merge created files
use defects.dta, clear

*create age
format Dateofbirth %td
g age=date-Dateofbirth
*create tenure
format Dateofjoin %td
g tenure=date-Dateofjoin
*create %female
g female=((workers-male)/workers)*100
*create defects per worker (total)
g defect_worker=(defect/workers)*100
*create defects per worker (mean)
g defect_worker2=(defect/workers2)*100
*create defects per unit of output
g defect_output=(defect/prod)*100

g counter=.
replace counter=time-343 if QMP==1
replace counter=344-time if QMP==0

g index=0
recode index 0=1 if counter<=15
recode index 0=2 if counter<=30
recode index 0=3 if counter<=45
recode index 0=4 if counter<=60
recode index 0=5 if counter<=75
recode index 0=6 if counter<=90
recode index 0=7 if counter<=105
recode index 0=8 if counter<=120
recode index 0=9 if counter<=135
recode index 0=10 if counter<=150
recode index 0=11 if counter<=165
recode index 0=12 if counter<=180
recode index 0=13 if counter<=195
recode index 0=14
la def index 1"15 day window" 2"30 day window" 3"45 day window" 4"60 day window" 5 "75 day window" 6 "90 window" 7 "105 days" ///
8 "120 days" 9 "135 days" 10 "150 days" 11 "165 days" 12 "180 days" 13 "195 days" 14 ">210 days", replace
la val index index

g win=0
replace win=counter if counter>=15 & counter<344

cd "C:\EDCC revision\Final\Stata code & data\"

twoway (kdensity defect if QMP==0, lcolor(red)) (kdensity defect if QMP==1, lcolor(blue)), ///
legend(off) xtitle("") ytitle("") ylabel(,labsize(vsmall) angle(45)) xlabel(,labsize(vsmall))

*FIGURE 3
hist defect, percent normal xtitle("number of defects", size(small)) ytitle("percent", size(small)) ///
ylabel(0(1)15,labsize(vsmall) angle(45)) xlabel(0(50)850,labsize(vsmall))
graph save Figure3.gph, replace

*normality test
swilk defect

cd "C:\EDCC revision\Final\Stata code & data\"

tsset time 
log using table7.log, replace
*TABLE 7
*NUMBER OF DEFECTS
*OLS
reg defect workers tenure age female QMP
estat dwatson
*Newey-West
newey defect workers tenure age female QMP, lag(1)
*Poisson
poisson defect workers tenure age female QMP
estat gof
*negative binomial
nbreg defect workers tenure age female QMP
*glm poisson with Newey-West standard errors
glm defect workers tenure age female QMP, family(poisson) vce(hac nwest)
*due to over-disperson negative binomial
glm defect workers tenure age female QMP, family(nbinomial ml) vce(hac nwest)
log close

swilk defect_worker
swilk defect_output

tsset time
log using table8.log, replace
*TABLE 8
*DEFECTS PER WORKER
*OLS
reg defect_worker tenure age female QMP
estat dwatson
*Newey-West
newey defect_worker tenure age female QMP, lag(1)
*DEFECTS PER UNIT OF OUTPUT
*OLS
reg defect_output workers tenure age female QMP
estat dwatson
*Newey-West
newey defect_output workers tenure age female QMP, lag(1)
log close

g win=0
recode win 0=1 if counter<=15
recode win 0=2 if counter<=30
recode win 0=3 if counter<=45
recode win 0=4 if counter<=60
recode win 0=5 if counter<=75
recode win 0=6 if counter<=90
recode win 0=7 if counter<=105
recode win 0=8 if counter<=120
recode win 0=9 if counter<=135
recode win 0=10 if counter<=150
recode win 0=11 if counter<=165
recode win 0=12 if counter<=180
recode win 0=13 if counter<=195
recode win 0=14 if counter<=210
recode win 0=15 if counter<=225
recode win 0=16 if counter<=240
recode win 0=17 if counter<=255
recode win 0=18 if counter<=270
recode win 0=19 if counter<=285
recode win 0=20 if counter<=300
recode win 0=21 if counter<=315
recode win 0=22 if counter<=330
recode win 0=23

g time1=time
cd "C:\EDCC revision\Final\Stata code & data\"
save "quality.dta", replace

*1. Defects
use "quality.dta", clear
tsset time
*glm defect ib(23).win##c.QMP workers tenure age female, family(poisson) vce(hac nwest)
*newey defect ib(23).win##c.QMP workers tenure age female, lag(1)
poisson defect ib(23).win##c.QMP workers tenure age female if counter<344

mat qmp=e(b)[1, 24..24]
mat b=e(b)[1, 25..46]
mat b=b'
local i=1
while `i'<=23 {
	mat int_`i'= b[`i',1]
	mat beta_`i'= qmp + int_`i'
		local i=`i'+1
	}

mat b=beta_1

local i=2
while `i'<=22 {
	matrix rowjoinbyname b = b beta_`i'
	mat b=b
	local i=`i'+1
}

svmat b, names(beta)
ren beta1 beta
replace beta=exp(beta-1)*100

g day=15
local i=2
while `i'<24 {
	replace day=day[_n-1]+15 if days==`i'
	local i=`i'+1
}

twoway (fpfitci beta day, ciplot(rbar) lcolor(blue) lwidth(medthick)), ytitle(reduction in the number of daily defects) ytitle(, size(medsmall)) ylabel(17(2)50, labsize(medsmall)) xtitle(number of days either side intervention) xlabel(15(15)330, labsize(medsmall) angle(vertical))
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\EDCC revision\F4A.gph", replace
*twoway (lpolyci beta day, n(28) lcolor(blue) lwidth(medthick)), ytitle(reduction in the number of daily defects) ytitle(, size(medsmall)) ylabel(15(5)50, labsize(medsmall)) xtitle(number of days either side intervention) xlabel(15(15)345, labsize(medsmall) angle(vertical))

*2. Defects per worker
use "quality.dta", clear
tsset time
newey defect_worker ib(23).win##c.QMP tenure age female if counter<344, lag(1)
mat qmp=e(b)[1, 24..24]
mat b=e(b)[1, 25..46]
mat b=b'
local i=1
while `i'<=23 {
	mat int_`i'= b[`i',1]
	mat beta_`i'= qmp + int_`i'
		local i=`i'+1
	}

mat b=beta_1

local i=2
while `i'<=22 {
	matrix rowjoinbyname b = b beta_`i'
	mat b=b
	local i=`i'+1
}

svmat b, names(beta)
ren beta1 beta

g day=15
local i=2
while `i'<24 {
	replace day=day[_n-1]+15 if days==`i'
	local i=`i'+1
}

twoway (fpfitci beta day, ciplot(rcap) lcolor(blue) lwidth(medthick)), ytitle(daily defects per worker) ytitle(, size(medsmall)) ylabel(-10(0.5)2, labsize(medsmall)) xtitle(number of days either side intervention) xlabel(15(15)330, labsize(medsmall) angle(vertical))
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\F4C.gph", replace
*twoway (lpoly beta day, n(28) lcolor(blue) lwidth(medthick)), ytitle(reduction in the number of daily defects) ytitle(, size(medsmall)) ylabel(-10(1)2, labsize(medsmall)) xtitle(number of days either side intervention) xlabel(15(15)345, labsize(medsmall) angle(vertical))

*3. Defects per unit of ouput
use "quality.dta", clear
tsset time
newey defect_output ib(23).win##c.QMP workers tenure age female if counter<344, lag(1)
mat qmp=e(b)[1, 24..24]
mat b=e(b)[1, 25..46]
mat b=b'
local i=1
while `i'<=23 {
	mat int_`i'= b[`i',1]
	mat beta_`i'= qmp + int_`i'
		local i=`i'+1
	}

mat b=beta_1

local i=2
while `i'<=22 {
	matrix rowjoinbyname b = b beta_`i'
	mat b=b
	local i=`i'+1
}

svmat b, names(beta)
ren beta1 beta

g day=15
local i=2
while `i'<24 {
	replace day=day[_n-1]+15 if days==`i'
	local i=`i'+1
}

twoway (fpfitci beta day, ciplot(rcap) lcolor(blue) lwidth(medthick)), ytitle(daily defects per unit of output) ytitle(, size(medsmall)) ylabel(-6(1)4, labsize(medsmall)) xtitle(number of days either side intervention) xlabel(15(15)330, labsize(medsmall) angle(vertical))
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\F4B.gph", replace
*twoway (lpoly beta day, n(28) lcolor(blue) lwidth(medthick)), ytitle(reduction in the number of daily defects) ytitle(, size(medsmall)) ylabel(-6(1)4, labsize(medsmall)) xtitle(number of days either side intervention) xlabel(15(15)345, labsize(medsmall) angle(vertical))

graph combine "C:\EDCC revision\Final\Stata code & data\F4A.gph" ///
"C:\EDCC revision\Final\Stata code & data\F4B.gph" ///
"C:\EDCC revision\Final\Stata code & data\F4C.gph"
graph save Figure4.gph, replace

******************************************************************************************************************************************************************************************

*C) OUTPUT VARIABLES NET OF DEFECTS - DAILY AGGREGATES
cd "C:\EDCC revision\Final\Stata code & data\"
use quantity.dta, clear

egen time=group(edate)
g date_1=mdy(8, 1, 2013)+time-1
format date %td
sort time

merge time using "C:\EDCC revision\Final\Stata code & data\defects.dta"
drop if _merge==1
drop _merge

*generate line index
g line_index=.
recode line_index .=1 if dummy11==1
recode line_index .=2 if dummy12==1
recode line_index .=3 if dummy21==1
recode line_index .=4 if dummy22==1
recode line_index .=5 if dummy31==1
recode line_index .=6 if dummy32==1
recode line_index .=7 if dummy41==1
recode line_index .=8 if dummy51==1
recode line_index .=9 if dummy61==1
la def line_index 1 "LINE 1A" 2 "LINE 1B" 3 "LINE 2A" 4 "LINE 2B" 5 "LINE 3A" 6 "LINE 3B" 7 "LINE 4" 8 "LINE 5" 9 "LINE 6", replace
la val line_index line_index

*generate daily variables
*1) daily production
g prod=exp(lnprod)
recode prod 1=0
bys time: egen daily_prod=sum(prod)
*2) daily workers
g workers=exp(lnworkers)
recode workers 1=0
bys time: egen daily_workers=sum(workers)
g log_daily_workers=log(daily_workers+1)
*3) daily smv
bys time: egen daily_smv=sum(smv)
g log_daily_smv=log(daily_smv+1)
*4) daily target
bys time: egen daily_target=sum(target)
g log_daily_target=log(daily_target+1)
* daily materials
g materials=exp(lnmaterials)
recode materials 1=0
bys time: egen daily_materials=sum(materials)
g log_daily_materials=log(daily_materials+1)

g a=1
bys time: gen b=sum(a)
keep if b==1

*generate net output variables
*1) production net of defects
g netprod=daily_prod-defect
g lnetprod=log(netprod+1)
*2) output per worker net of defects
g netavprod=netprod/daily_workers
g lnetavprod=log(netavprod+1)
*3) productivity net of defects
g netproductivity=((netprod)*daily_smv)/(daily_worker*8*60)
g lnetproductivity=log(netproductivity+1)
*4) net target efficiency
g nettareff=((daily_target-defect)*daily_smv)/(daily_worker*8*60)
g lnettareff=log(nettareff+1)

g counter=.
replace counter=time-343 if QMP==1
replace counter=344-time if QMP==0

g win=0
recode win 0=1 if counter<=15
recode win 0=2 if counter<=30
recode win 0=3 if counter<=45
recode win 0=4 if counter<=60
recode win 0=5 if counter<=75
recode win 0=6 if counter<=90
recode win 0=7 if counter<=105
recode win 0=8 if counter<=120
recode win 0=9 if counter<=135
recode win 0=10 if counter<=150
recode win 0=11 if counter<=165
recode win 0=12 if counter<=180
recode win 0=13 if counter<=195
recode win 0=14 if counter<=210
recode win 0=15 if counter<=225
recode win 0=16 if counter<=240
recode win 0=17 if counter<=255
recode win 0=18 if counter<=270
recode win 0=19 if counter<=285
recode win 0=20 if counter<=300
recode win 0=21 if counter<=315
recode win 0=22 if counter<=330
recode win 0=23
cd "C:\EDCC revision\Final\Stata code & data\"
save "net_firm.dta", replace

tsset time
cd "C:\EDCC revision\Final\Stata code & data\"
log using table9.log, replace
*TABLE 9
*net output
newey lnetprod log_daily_workers log_daily_materials log_daily_smv log_daily_target QMP timedummy*, lag(1) 
qui: reg lnetprod log_daily_workers log_daily_materials log_daily_smv log_daily_target QMP timedummy*
estat dwatson
*labour productivity - FE
newey lnetavprod log_daily_materials log_daily_smv log_daily_target QMP timedummy*, lag(1) 
qui: reg lnetavprod log_daily_materials log_daily_smv log_daily_target QMP timedummy*
estat dwatson
*Menzel productivity - FE
newey lnetproductivity log_daily_materials log_daily_target QMP timedummy*, lag(1)
qui: reg lnetproductivity log_daily_materials log_daily_target QMP timedummy*
estat dwatson
*efficiency - FE
newey lnettareff log_daily_materials QMP timedummy*, lag(1) 
qui: reg lnettareff log_daily_materials log_daily_smv log_daily_target QMP timedummy*
estat dwatson
log close

*WINDOWS LENGTHS
*A)window - net ouput
cd "C:\EDCC revision\Final\Stata code & data\"
use "net_firm.dta", clear
tsset time

newey lnetprod ib(23).win##c.QMP log_daily_workers log_daily_materials log_daily_smv log_daily_target if counter<344, lag(1)
margins, dydx(QMP) over(win)
mat a=r(table)
mat a=a'
mat beta=a[1..22, 1]
mat lower=a[1..22, 5]
mat upper=a[1..22, 6]
clear
svmat beta, names(b)
svmat lower, names(l)
svmat upper, names(u)
ren b1 b
ren l1 l
ren u1 u
g win=_n
replace win=win*15

twoway (fpfitci b win, ciplot(rcap) lcolor(blue) lwidth(medthick)), ytitle(daily output net of defects) ytitle(, size(small)) ylabel(-0.2(0.1)0.6, labsize(medsmall)) xtitle(number of days either side intervention) xlabel(15(15)330, labsize(medsmall) angle(vertical)) yline(0,lcolor(red))
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\F5A.gph", replace

*window - net ouput per worker
cd "C:\EDCC revision\Final\Stata code & data\"
use "net_firm.dta", clear
tsset time

newey lnetavprod ib(23).win##c.QMP lnworkers lnmaterials lnsmv lntarget if counter<344, lag(1)
margins, dydx(QMP) over(win)
mat a=r(table)
mat a=a'
mat beta=a[1..22, 1]
mat lower=a[1..22, 5]
mat upper=a[1..22, 6]
clear
svmat beta, names(b)
svmat lower, names(l)
svmat upper, names(u)
ren b1 b
ren l1 l
ren u1 u
g win=_n
replace win=win*15
twoway (fpfitci b win, ciplot(rcap) lcolor(blue) lwidth(medthick)), ytitle(daily output per worker net of defects) ytitle(, size(small)) ylabel(-0.2(0.1)0.2, labsize(medsmall)) xtitle(number of days either side intervention) xlabel(15(15)330, labsize(medsmall) angle(vertical)) yline(0,lcolor(red))
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\F5B.gph", replace

*window - net target efficiency
cd "C:\EDCC revision\Final\Stata code & data\"
use "net_firm.dta", clear
tsset time

newey lnettareff ib(23).win##c.QMP lnmaterials if counter<344, lag(1)
margins, dydx(QMP) over(win)
mat a=r(table)
mat a=a'
mat beta=a[1..22, 1]
mat lower=a[1..22, 5]
mat upper=a[1..22, 6]
clear
svmat beta, names(b)
svmat lower, names(l)
svmat upper, names(u)
ren b1 b
ren l1 l
ren u1 u
g win=_n
replace win=win*15
twoway (fpfitci b win, ciplot(rcap) lcolor(blue) lwidth(medthick)), ytitle(daily target efficiency net of defects) ytitle(, size(small)) ylabel(-0.2(0.1)0.6, labsize(medsmall)) xtitle(number of days either side intervention) xlabel(15(15)330, labsize(medsmall) angle(vertical)) yline(0,lcolor(red))
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\F5C.gph", replace

*window - net productivity
cd "C:\EDCC revision\Final\Stata code & data\"
use "net_firm.dta", clear
tsset time

newey lnetproductivity ib(23).win##c.QMP log_daily_materials log_daily_target if counter<344, lag(1)
margins, dydx(QMP) over(win)
mat a=r(table)
mat a=a'
mat beta=a[1..22, 1]
mat lower=a[1..22, 5]
mat upper=a[1..22, 6]
clear
svmat beta, names(b)
svmat lower, names(l)
svmat upper, names(u)
ren b1 b
ren l1 l
ren u1 u
g win=_n
replace win=win*15
/*produces confidence intervals
la def win 1 "5" 2"15" 3"30" 4"45" 5"60" 6"75" 7"90" 8"105" 9"120" 10"135" 11"150" 12"165" 13"180" 14"195" 15"210" 16"225" 17"240" ///
18"255" 19"270" 20"285" 21"300" 22"315" 23"330", replace
la val win win
recode win 1=0 2=15 3=30 4=45 5=60 6=75 7=90 8=105 9=120 10=135 11=150 12=165 13=180 14=195 15=210 16=225 17=240 18=255 19=270 20=285 21=300 22=315 23=330
*/
twoway (fpfit l win, lcolor(gs10)) (fpfit u win, lcolor(gs10)) (fpfit b win, lcolor(blue) lwidth(medthick)), ytitle(daily productivity net of defects) ytitle(, size(small)) ///
xtitle(number of days either side intervention) xlabel(0(15)330, labsize(small) angle(vertical)) yline(0, lcolor(red))
twoway (fpfitci b win, ciplot(rcap) lcolor(blue) lwidth(medthick)), ytitle(daily productivity net of defects) ytitle(, size(small)) ylabel(-0.2(0.1)0.4, labsize(medsmall)) xtitle(number of days either side intervention) xlabel(15(15)330, labsize(medsmall) angle(vertical)) yline(0,lcolor(red))
graph save "Graph" "C:\EDCC revision\Final\Stata code & data\F5D.gph", replace

*FIGURE 5
graph combine "C:\EDCC revision\Final\Stata code & data\F5A.gph" ///
"C:\EDCC revision\Final\Stata code & data\F5B.gph" ///
"C:\EDCC revision\Final\Stata code & data\F5C.gph" ///
"C:\EDCC revision\Final\Stata code & data\F5D.gph" 
graph save Figure5.gph, replace


******************************************************************************************************************************************************************************************

*D) PIECE RATES

cd "C:\EDCC revision\Final\Stata code & data\"

use "piece_rates.dta", clear

*TABLE B1
log using "piece_rate_analysis.log", replace
*Fixed effects analysis
*log defects & QMP
qui: xtreg log_pay defect QMP age age2 ten ten2 male ib(2013).year ib(1).month ib(1).day, i(id) fe
est store FE1
*Interaction
qui: xtreg log_pay c.defect##ib(0).QMP age age2 ten ten2 ib(2013).year ib(1).month ib(1).day, i(id) fe
est store FE2
*Random effects analysis
*log defects & QMP
qui: xtreg log_pay defect QMP male age age2 ten ten2 ib(2013).year ib(1).month ib(1).day, i(id)
est store RE1
*Interaction
qui: xtreg log_pay c.defect##ib(0).QMP male age age2 ten ten2 ib(2013).year ib(1).month ib(1).day, i(id)
est store RE2
estout FE1 RE1, cells(b(star fmt(%9.4f)) se(par))  stats(r2_w N, fmt(%9.4f %9.0g) labels(R-squared)) keep(defect QMP age* ten* male _cons)
log close

******************************************************************************************************************************************************************************************

*E) APPENDIX TABLES A1 to A4
cd "C:\karl's files\Management Practice - Pakistan\EDCC revision\Final\Stata code & data\"
use panel_long.dta, clear

drop if code==.
g a=1
bys line code: gen b=sum(a) 
bys product line: egen smv_line_product_total=sum(smv) /*total smv by line and product*/
bys product: egen totproc=sum(a)
ta line, gen(Line)
bys product: egen line_1=sum(Line1) 
bys product: egen line_2=sum(Line2)
bys product: egen line_3=sum(Line3)
bys product: egen line_4=sum(Line4)
bys product: egen line_5=sum(Line5)
bys product: egen line_6=sum(Line6)
keep if b==1 /*gets rid of duplicates*/

drop b
bys code: egen b=sum(a) /*counts code gives value between 1 and 6***/

*1) product details - for each product: smv by line and # of processes 
ta line, gen(line_n)
bys product: egen smv_prod=sum(smv) /*total smv by product*/
bys product: egen smv_prod_line1=max(smv_line_product_total) if line_n1==1/*total smv by product for line 1*/
bys product: egen smv_prod_line2=max(smv_line_product_total) if line_n2==1/*total smv by product for line 2*/
bys product: egen smv_prod_line3=max(smv_line_product_total) if line_n3==1/*total smv by product for line 3*/
bys product: egen smv_prod_line4=max(smv_line_product_total) if line_n4==1/*total smv by product for line 4*/
bys product: egen smv_prod_line5=max(smv_line_product_total) if line_n5==1/*total smv by product for line 5*/
bys product: egen smv_prod_line6=max(smv_line_product_total) if line_n6==1/*total smv by product for line 6*/

bys product: egen smv_prod_line1_total=max(smv_prod_line1)
bys product: egen smv_prod_line2_total=max(smv_prod_line2)
bys product: egen smv_prod_line3_total=max(smv_prod_line3)
bys product: egen smv_prod_line4_total=max(smv_prod_line4)
bys product: egen smv_prod_line5_total=max(smv_prod_line5)
bys product: egen smv_prod_line6_total=max(smv_prod_line6)

egen processes=group(code)
ta processes, gen(proc)

bys product: gen d=sum(a)
keep if d==1
sort product
br product totproc line_* smv_prod_line*_total

*2) TABLES A1 to A4
use panel_long.dta, clear

drop if code==.
g a=1
bys line code: gen b=sum(a) 
keep if b==1 /*gets rid of duplicates*/

drop b
bys code: egen b=sum(a) /*counts code gives value between 1 and 6***/

*A1
bys line: egen smv_line_total=sum(smv) /*total smv by line*/
bys line code: egen smv_line_code_total=sum(smv) /*total smv by line and code*/
bys code: egen smv_code_total=mean(smv) /*average smv by code*/

ta code if b==6 /*common operations across lines*/
bys line: ta code if b==1 /*line specific operations*/
bys code: gen c=sum(a) /*identifier for overall duplicate codes*/

/*

bys product: egen line_1=max(line_n1)
bys product: egen line_2=max(line_n2)
bys product: egen line_3=max(line_n3)
bys product: egen line_4=max(line_n4)
bys product: egen line_5=max(line_n5)
bys product: egen line_6=max(line_n6)
bys product: gen d=sum(a)
keep if d==1
drop line_n*
li product line_* smv_prod_line*_total
*/

*A3
log using common.log, replace
list code desc smv if b==6 & c==1
log close

*A2
log using unique.log, replace
bys line: list code desc smv if b==1 & c==1
log close

log using unique_1.log, replace
list code desc smv if b==1 & c==1 & line==1
log close
log using unique_2.log, replace
list code desc smv if b==1 & c==1 & line==2
log close
log using unique_3.log, replace
list code desc smv if b==1 & c==1 & line==3
log close
log using unique_4.log, replace
list code desc smv if b==1 & c==1 & line==4
log close
log using unique_5.log, replace
list code desc smv if b==1 & c==1 & line==5
log close
log using unique_6.log, replace
list code desc smv if b==1 & c==1 & line==6
log close

drop if b==1 | b==6
*keep if c==1
g str_desc=substr(desc, 1,100)
compress

program define leftalign

    version 9.2
    
    syntax [varlist] , [Label All]
    
    foreach v of varlist `varlist' {
    
        local f : format `v'
        
        local ff = regexr("`f'", "%~?\-?","")
        
        if regexm("`f'", "%.+s") local cmd cap format %-`ff' `v'
        
        if "`: value label `v''" != "" & "`label'" != "" ///
            local cmd cap format %-`ff' `v'
        
        if "`all'" != "" local cmd cap format %-`ff' `v'
        
        `cmd'
        
        local ff : format `v'
        if "`ff'" != "`f'" local vlist `vlist' `v'

    }
    
    if "`vlist'" != "" des `vlist'
        
end

leftalign, label


foreach v in line code str_desc smv {
    char `v'[varname] "`: var label `v''"
}

ta line, gen(line_n)

log using other_p1.log, replace
sort code line
list code line_n* str_desc, table clean compress subvarname abbrev(100)
log close

log using other_p2.log, replace
sort code line
list code line_n* smv, table clean compress subvarname abbrev(100)
log close

**

use other_p1.dta, clear
merge code using other_p2.dta
drop _merge

bys code: egen line_1=max(line_n1)
bys code: egen line_2=max(line_n2)
bys code: egen line_3=max(line_n3)
bys code: egen line_4=max(line_n4)
bys code: egen line_5=max(line_n5)
bys code: egen line_6=max(line_n6)

g a=1
bys code: gen d=sum(a)
keep if d==1
drop a d line_n*

g line="1,2" if line_1==1 & line_2==1
replace line="1,3" if line_1==1 & line_3==1
replace line="1,4" if line_1==1 & line_4==1
replace line="1,5" if line_1==1 & line_5==1
replace line="1,6" if line_1==1 & line_6==1

replace line="2,3" if line_2==1 & line_3==1
replace line="2,4" if line_2==1 & line_4==1
replace line="2,5" if line_2==1 & line_5==1
replace line="2,6" if line_2==1 & line_6==1

replace line="3,4" if line_3==1 & line_4==1
replace line="3,5" if line_3==1 & line_5==1
replace line="3,6" if line_3==1 & line_6==1

replace line="4,5" if line_4==1 & line_5==1
replace line="4,6" if line_4==1 & line_6==1

replace line="5,6" if line_5==1 & line_6==1

replace line="1,2,3" if line_1==1 & line_2==1 & line_3==1
replace line="1,2,4" if line_1==1 & line_2==1 & line_4==1
replace line="1,2,5" if line_1==1 & line_2==1 & line_5==1
replace line="1,2,6" if line_1==1 & line_2==1 & line_6==1

replace line="1,3,4" if line_1==1 & line_3==1 & line_4==1
replace line="1,3,5" if line_1==1 & line_3==1 & line_5==1
replace line="1,3,6" if line_1==1 & line_3==1 & line_6==1

replace line="1,4,5" if line_1==1 & line_4==1 & line_5==1
replace line="1,4,6" if line_1==1 & line_4==1 & line_6==1

replace line="2,3,4" if line_2==1 & line_3==1 & line_4==1
replace line="2,3,5" if line_2==1 & line_3==1 & line_5==1
replace line="2,3,6" if line_2==1 & line_3==1 & line_6==1

replace line="2,4,5" if line_2==1 & line_4==1 & line_5==1
replace line="2,4,6" if line_2==1 & line_4==1 & line_6==1

replace line="3,4,5" if line_3==1 & line_4==1 & line_5==1
replace line="3,4,6" if line_3==1 & line_4==1 & line_6==1

replace line="3,5,6" if line_3==1 & line_5==1 & line_6==1

replace line="4,5,6" if line_4==1 & line_5==1 & line_6==1

replace line="1,2,3,4" if line_1==1 & line_2==1 & line_3==1 & line_4==1
replace line="1,2,3,5" if line_1==1 & line_2==1 & line_3==1 & line_5==1
replace line="1,2,3,6" if line_1==1 & line_2==1 & line_3==1 & line_6==1

replace line="2,3,4,5" if line_2==1 & line_3==1 & line_4==1 & line_5==1
replace line="2,3,4,6" if line_2==1 & line_3==1 & line_4==1 & line_6==1

replace line="2,4,5,6" if line_2==1 & line_4==1 & line_5==1 & line_6==1

replace line="3,4,5,6" if line_3==1 & line_4==1 & line_5==1 & line_6==1

replace line="1,2,3,4,5" if line_1==1 & line_2==1 & line_3==1 & line_4==1 & line_5==1
replace line="1,2,3,4,6" if line_1==1 & line_2==1 & line_3==1 & line_4==1 & line_6==1

replace line="1,3,4,5,6" if line_1==1 & line_3==1 & line_4==1 & line_5==1 & line_6==1

replace line="2,3,4,5,6" if line_2==1 & line_3==1 & line_4==1 & line_5==1 & line_6==1

replace line="1,2,3,4,5,6" if line_1==1 & line_2==1 & line_3==1 & line_4==1 & line_5==1 & line_6==1

br code line line_* str_desc

edit line code str_desc smv